package com.example.myfirstapp;

import com.parse.ParseUser;
import android.os.Bundle;
import android.app.ActionBar;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.app.ActionBar.Tab;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.support.v4.app.NavUtils;

public class HomeActivity extends Activity {
	public static ParseUser currentUser; 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        ParseUser cUser = ParseUser.getCurrentUser();
        if (cUser != null) {
        	currentUser = cUser;
        } else {
        	currentUser = null;
        }
        
	    final ActionBar ab = getActionBar();
	    ab.setNavigationMode( ActionBar.NAVIGATION_MODE_TABS );
	    
	    ab.addTab( 
    		ab.newTab()
		        .setText( R.string.frag_title_messages )
		        .setTabListener( 
						new MyTabListener( this, MessagesFragment.class.getName()) 
				) 
		);
	    ab.addTab( 
    		ab.newTab()
		        .setText( R.string.frag_title_friends )
		        .setTabListener( 
						new MyTabListener( this, FriendsFragment.class.getName()) 
				) 
		);
	    ab.addTab( 
    		ab.newTab()
		        .setText( R.string.frag_title_settings )
		        .setTabListener( 
						new MyTabListener( this, SettingsFragment.class.getName()) 
				) 
		); 	 
	
        
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_home, menu);
        return true;
    }

    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                NavUtils.navigateUpFromSameTask(this);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
    
    private class MyTabListener implements ActionBar.TabListener {
        private Fragment mFragment;
    	private final Activity mActivity;
		private final String mFragName;

        public MyTabListener(Activity activity, String fragName) {
			mActivity = activity;
			mFragName = fragName;
        }

        public void onTabSelected(Tab tab, FragmentTransaction ft) {
        	mFragment = Fragment.instantiate( mActivity, mFragName );
			ft.add( android.R.id.content, mFragment );
            
        }

        public void onTabUnselected(Tab tab, FragmentTransaction ft) {
            ft.remove(mFragment);
			mFragment = null;

        }

        public void onTabReselected(Tab tab, FragmentTransaction ft) {
            
        }

    }
}
